package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysClassificationMapper;
import com.ruoyi.system.domain.SysClassification;
import com.ruoyi.system.service.ISysClassificationService;

/**
 * 药品分类Service业务层处理
 * 
 * @author jiashu
 * @date 2025-03-13
 */
@Service
public class SysClassificationServiceImpl implements ISysClassificationService 
{
    @Autowired
    private SysClassificationMapper sysClassificationMapper;

    /**
     * 查询药品分类
     * 
     * @param classificationId 药品分类主键
     * @return 药品分类
     */
    @Override
    public SysClassification selectSysClassificationByClassificationId(Long classificationId)
    {
        return sysClassificationMapper.selectSysClassificationByClassificationId(classificationId);
    }

    /**
     * 查询药品分类列表
     * 
     * @param sysClassification 药品分类
     * @return 药品分类
     */
    @Override
    public List<SysClassification> selectSysClassificationList(SysClassification sysClassification)
    {
        return sysClassificationMapper.selectSysClassificationList(sysClassification);
    }

    /**
     * 新增药品分类
     * 
     * @param sysClassification 药品分类
     * @return 结果
     */
    @Override
    public int insertSysClassification(SysClassification sysClassification)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        sysClassification.setCreateBy(loginUser.getUsername());
        sysClassification.setCreateTime(DateUtils.getNowDate());
        return sysClassificationMapper.insertSysClassification(sysClassification);
    }

    /**
     * 修改药品分类
     * 
     * @param sysClassification 药品分类
     * @return 结果
     */
    @Override
    public int updateSysClassification(SysClassification sysClassification)
    {
        return sysClassificationMapper.updateSysClassification(sysClassification);
    }

    /**
     * 检查修改的分类名是否可用  true 可用   false不可用
     * @param sysClassification
     * @return
     */
    @Override
    public boolean checkClassificationName(SysClassification sysClassification) {
        boolean result = false;
        SysClassification vo = new SysClassification();
        vo.setClassificationName(sysClassification.getClassificationName());
        List<SysClassification> list = selectSysClassificationList(vo);
        if (list.isEmpty()){
            result = true;  // 名称可用
        } else if (list.size() ==1 && list.get(0).getClassificationId() == sysClassification.getClassificationId()) {
            return true;
        }

        return result;
    }

    /**
     * 批量删除药品分类
     * 
     * @param classificationIds 需要删除的药品分类主键
     * @return 结果
     */
    @Override
    public int deleteSysClassificationByClassificationIds(Long[] classificationIds)
    {
        int result = 0;
        // 不允许删除根节点
        for (Long classificationId : classificationIds) {
            SysClassification classification = selectSysClassificationByClassificationId(classificationId);
            if (classification.getClassificationId() == 1) {

            }else {
                int i = sysClassificationMapper.deleteSysClassificationByClassificationId(classificationId);
                result +=i;
            }
        }
        return result;
    }

    /**
     * 删除药品分类信息
     * 
     * @param classificationId 药品分类主键
     * @return 结果
     */
    @Override
    public int deleteSysClassificationByClassificationId(Long classificationId)
    {
        return sysClassificationMapper.deleteSysClassificationByClassificationId(classificationId);
    }

    /**
     * 根节点递归
     * @return
     */
    @Override
    public List<SysClassification> selectRootClassification() {
        return sysClassificationMapper.selectRootClassification();
    }

    /**
     * 根据某一节点获取对应的递归信息
     * @param classificationId
     * @return
     */
    @Override
    public List<SysClassification> selectChildrenByParentId(Long classificationId) {
        return sysClassificationMapper.selectChildrenByParentId(classificationId);
    }
}
